// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MapTile.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Localization
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.Serialization;

    /// <summary>
    /// Represents a fixed-size portion of the global map.
    /// </summary>
    [DataContract]
    internal struct MapTile
    {
        /// <summary>
        /// The size of the tiles that make up the map, as log value.
        /// </summary>
        public const byte LogTileSize = 6;

#if DEBUG
        /// <summary>
        /// The tile owner.
        /// </summary>
        public GlobalMap Owner;
#endif

        /// <summary>
        /// Indicates whether this tile is read-only (false) or read-write (true).
        /// The logic that uses this field only works as long as MapTile is a struct (as in, each map has its copy of the MapTile metadata).
        /// </summary>
        [DataMember]
        private bool isWritable;

        /// <summary>
        /// The array of cells.
        /// </summary>
        [DataMember]
        private MapCell[] cells;

        /// <summary>
        /// Gets or sets a value indicating whether this copy of the map is read-only (false) or read-write (true).
        /// This is used in determining if a copy of this tile needs to be made before writing to it.
        /// </summary>
        public bool IsWritable
        {
            get
            {
                return this.isWritable;
            }

            internal set
            {
                this.isWritable = value;
            }
        }

        /// <summary>
        /// Gets or sets the corresponding entry. 
        /// </summary>
        /// <param name="x">The vertical coordinate.</param>
        /// <param name="y">The horizontal coordinate.</param>
        /// <returns>The value of the cell.</returns>
        public MapCell this[int x, int y]
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get
            {
                if (this.cells == null)
                {
                    return default(MapCell);
                }

                int index = (x << LogTileSize) + y;
                return this.cells[index];
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            set
            {
                if (this.cells == null)
                {
                    this.cells = new MapCell[1 << (LogTileSize + LogTileSize)];
                    this.isWritable = true;
                }

                if (!this.isWritable)
                {
                    throw new InvalidOperationException("Attempting to write a protected tile.");
                }

                int index = (x << LogTileSize) + y;
                this.cells[index] = value;
            }
        }

        /// <summary>
        /// Creates a writable copy of this tile.
        /// </summary>
        /// <returns>A writable copy.</returns>
        public MapTile Clone()
        {
            MapTile newTile = new MapTile();
            if (this.cells != null)
            {
                newTile.cells = (MapCell[])this.cells.Clone();
            }

            newTile.isWritable = true;
            return newTile;
        }
    }
}
